package com.wudizaba.jasper.Util;


import cn.hutool.core.io.resource.ResourceUtil;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.SneakyThrows;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.export.JRGraphics2DExporter;
import net.sf.jasperreports.engine.export.JRGraphics2DExporterParameter;
import net.sf.jasperreports.engine.export.ooxml.JRDocxExporter;
import net.sf.jasperreports.engine.export.ooxml.JRXlsxExporter;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
import net.sf.jasperreports.export.SimpleExporterInput;
import net.sf.jasperreports.export.SimpleOutputStreamExporterOutput;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.InputStream;
import java.util.Map;

/**
 * jasper配置类
 */
@Component
@Data
@NoArgsConstructor
public class JasperUtil {

    private static final Logger log = LoggerFactory.getLogger(JasperUtil.class);

    /**
     * @param xmlPath       模板文件路径
     * @param jasperObjects 数据
     * @return
     */
    //TODO Jasper导出
    @SneakyThrows
    public void exportPdfFromJasper(String xmlPath, Map<String, Object> jasperObjects, String outPutPath, Integer en) {
        System.out.println(" 开始解析 ");
        InputStream stream = ResourceUtil.getStream(xmlPath);
        JasperPrint jasperPrint = jasperPrint(stream, jasperObjects);
        //导出文件到目标路径
        if (en == 1) {
            exportPdf(jasperPrint, outPutPath);
        } else if (en == 2) {
            exporthtml(jasperPrint, outPutPath);
        } else if (en == 3) {
            exportXml(jasperPrint, outPutPath);
        } else if (en == 4) {
            exportWord(jasperPrint, outPutPath);
        }else if (en == 5) {
            exportExcel(jasperPrint, outPutPath);
        }else if (en == 6) {
            exportImage(jasperPrint, outPutPath);
        }
        stream.close();//关闭流
    }

    /**
     * 解析pdf+html+xml
     */
    @SneakyThrows
    public JasperPrint jasperPrint(InputStream stream, Map<String, Object> jasperObjects) {
        //解析文件
        JasperDesign jasperDesign = JRXmlLoader.load(stream);
        JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
        //填充数据
        return JasperFillManager.fillReport(jasperReport, jasperObjects, new JREmptyDataSource());
    }

    /**
     * 导出pdf文件
     *
     * @param jasperPrint
     * @param outPutPath
     */
    @SneakyThrows
    public void exportPdf(JasperPrint jasperPrint, String outPutPath) {
        JasperExportManager.exportReportToPdfFile(jasperPrint, outPutPath);
    }

    /**
     * 导出html
     *
     * @param jasperPrint
     * @param outPutPath
     */
    @SneakyThrows
    public void exporthtml(JasperPrint jasperPrint, String outPutPath) {
        JasperExportManager.exportReportToHtmlFile(jasperPrint, outPutPath);
    }


    /**
     * 导出xml
     *
     * @param jasperPrint
     * @param outPutPath
     */
    @SneakyThrows
    public void exportXml(JasperPrint jasperPrint, String outPutPath) {
        JasperExportManager.exportReportToXmlFile(jasperPrint, outPutPath, false);
    }


    @SneakyThrows
    public void exportWord(JasperPrint jasperPrint, String outPutPath) {
        //设置导出word
        JRDocxExporter jrDocxExporter = new JRDocxExporter();
        SimpleExporterInput simpleExporterInput = new SimpleExporterInput(jasperPrint);
        jrDocxExporter.setExporterInput(simpleExporterInput);
        jrDocxExporter.setExporterOutput(new SimpleOutputStreamExporterOutput(outPutPath));
        jrDocxExporter.exportReport();
    }

    @SneakyThrows
    public void exportExcel(JasperPrint jasperPrint, String outPutPath) {
        //设置导出excel
        JRXlsxExporter jrXlsExporter = new JRXlsxExporter();
        SimpleExporterInput simpleExporterInput = new SimpleExporterInput(jasperPrint);
        jrXlsExporter.setExporterInput(simpleExporterInput);
        jrXlsExporter.setExporterOutput(new SimpleOutputStreamExporterOutput(outPutPath));
        jrXlsExporter.exportReport();
    }

    @SneakyThrows
    public void exportImage(JasperPrint jasperPrint, String outPutPath) {
        // //设置导出图片
        JRGraphics2DExporter jrGraphics2DExporter = new JRGraphics2DExporter();
        BufferedImage bufferedImage = new BufferedImage(jasperPrint.getPageWidth() * 4, jasperPrint.getPageHeight() * 4, BufferedImage.TYPE_3BYTE_BGR);
        Graphics2D graphics = (Graphics2D) bufferedImage.getGraphics();
        //设置图片大小 设置图片缩放比例
        jrGraphics2DExporter.setParameter(JRGraphics2DExporterParameter.GRAPHICS_2D, graphics);
        jrGraphics2DExporter.setParameter(JRGraphics2DExporterParameter.ZOOM_RATIO, Float.valueOf(4F));
        jrGraphics2DExporter.setParameter(JRGraphics2DExporterParameter.JASPER_PRINT,jasperPrint);
        jrGraphics2DExporter.exportReport();
        ImageIO.write(bufferedImage, "JPEG", new File(outPutPath));
        bufferedImage.flush();// 刷新
        graphics.dispose();//释放资源
    }
}
